import MonoSingle from "./MonoSingle";
declare let require: any
const { ccclass, property,menu } = cc._decorator;

@ccclass
@menu("Utility/InitClickEventToJs")
export default class InitClickEventToJs extends cc.Component {

	@property(cc.Node)
	targetNode: cc.Node = null;
	@property
	callBackName: string = "CloseCallBack";
	@property
	customEventData: string = "";
	params: [];
	functionAction:Function = null;
	start() {
		this.onClickEvent(this.node.getComponent(cc.Button), 'InitClickEventToJs', 'callBackEvent');
	}

	onClickEvent(btn: cc.Button, component: string, handler: string) {
		// btn.clickEvents = [];
		var clickEventHandler = new cc.Component.EventHandler();
		clickEventHandler.target = btn.node;
		clickEventHandler.component = component;
		clickEventHandler.handler = handler;
		clickEventHandler.customEventData = this.customEventData;
		btn.clickEvents.push(clickEventHandler);
	}
	callBackEvent(): void {
		switch (this.callBackName) {
			case "CloseCallBack":
				this.closeEvent();
				break;
			case "CloseGameCallBack":
				MonoSingle.GetBaseInputEvent().SetLeaveGameOnClick();
				break;
			case "FunctionCallBack":
				this.functionAction();
				break;
			default:
				break;
		}

	}
	closeEvent(): void {
		this.targetNode.getComponentsInChildren(cc.Button).forEach(element => {
			element.interactable = false;
		});
		if (this.targetNode && MonoSingle.GetTvBoxInput().getSavePrePanelTop() == this.targetNode)
			MonoSingle.GetTvBoxInput().popSavePrePanelTop()
		MonoSingle.GetTvBoxInput().initTvBoxCurPanelEvent(0, MonoSingle.GetTvBoxInput().getSavePrePanelTop())
	}
	onDestroy(): void {
		if (this.targetNode) {
			for (let i = 0; i < MonoSingle.GetTvBoxInput().savePrePanelList.length; i++) {
				let vaule = MonoSingle.GetTvBoxInput().savePrePanelList[i];
				if (vaule == this.targetNode) {
					MonoSingle.GetTvBoxInput().savePrePanelList.splice(i, 1), MonoSingle.GetTvBoxInput().initTvBoxCurPanelEvent(0, MonoSingle.GetTvBoxInput().getSavePrePanelTop())
				}
			}
		}
	}

}
